package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese01Economic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese03Econvalue;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class EconomicValueDao extends Dao {

    private static final String CLASS_NAME = EconomicValueDao.class.getName();

    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * 
     * @param economicIndexDef
     * @throws PersistenceException
     */
    public void createEconomicValue(Teese03Econvalue economicIndexDef)
            throws PersistenceException {

        Transaction tx = null;
        String METHOD_NAME = "createEconomicValue";
        log.entering(CLASS_NAME, METHOD_NAME);
        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(economicIndexDef);
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param ecoV
     * @throws PersistenceException
     */
    public void deleteEconomicValue(Teese03Econvalue ecoV)
            throws PersistenceException {

        String METHOD_NAME = "deleteEconomicValue";
        log.entering(CLASS_NAME, METHOD_NAME);
        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(ecoV);
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param ecoValue
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese03Econvalue> findEconomicValues(
            Teese03Econvalue ecoValue) throws PersistenceException {

        String METHOD_NAME = "findEconomicValues";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicValuesQuery",
                Teese03Econvalue.class);
        query.setParameters(ecoValue);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * 
     * @param ecoV
     * @return
     * @throws PersistenceException
     */
    public Teese03Econvalue getEcoValueForUpdate(Teese03Econvalue ecoV)
            throws PersistenceException {

        String METHOD_NAME = "getEcoValueForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teese03Econvalue result = null;
        Teese03Econvalue pk = new Teese03Econvalue();
        pk.setEese03CurrencyCodeC(ecoV.getEese03CurrencyCodeC());
        pk.setTeese01Economic(ecoV.getTeese01Economic());
        pk.setEese03EcvalueEffectivDateY(ecoV.getEese03EcvalueEffectivDateY());
        pk.setEese03PlantCodeC(ecoV.getEese03PlantCodeC());
        pk.setEese03EcvalueTypeCodeC(ecoV.getEese03EcvalueTypeCodeC());
        result = (Teese03Econvalue) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * 
     * @param ecoV
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese03Econvalue> FindEconomicMaxValuesQuery(
            Teese03Econvalue ecoV) throws PersistenceException {
        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicMaxValuesQuery",
                Teese03Econvalue.class);
        query.setParameters(ecoV);

        resultCol = (Collection) pm.executeQuery(query);

        return resultCol;
    }

    /**
     * 
     * @param ecoV
     * @return
     * @throws PersistenceException
     */
    public Collection<Teese03Econvalue> FindEconomicPeriodValuesQuery(
            Teese03Econvalue ecoV) throws PersistenceException {
        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicPeriodValuesQuery",
                Teese03Econvalue.class);
        query.setParameters(ecoV);

        resultCol = (Collection) pm.executeQuery(query);

        return resultCol;
    }

    public Teese03Econvalue getEcoValueById(Teese03Econvalue ecoV)
            throws PersistenceException {

        String METHOD_NAME = "getEcoValueForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teese03Econvalue result = null;
        Teese03Econvalue pk = new Teese03Econvalue();
        pk.setEese03CurrencyCodeC(ecoV.getEese03CurrencyCodeC());
        pk.setTeese01Economic(ecoV.getTeese01Economic());
        pk.setEese03EcvalueEffectivDateY(ecoV.getEese03EcvalueEffectivDateY());
        pk.setEese03PlantCodeC(ecoV.getEese03PlantCodeC());
        pk.setEese03EcvalueTypeCodeC(ecoV.getEese03EcvalueTypeCodeC());
        result = (Teese03Econvalue) super.getObjById(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    public Collection economicValuesUpdatedFindLessDateQuery(
            Teese03Econvalue ecoV, Timestamp periodSelec) throws PersistenceException {

        String METHOD_NAME = "economicValuesUpdatedFindLessDateQuery";
        log.entering(CLASS_NAME, METHOD_NAME);
        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicValuesUpdatedLessDate",
                Teese03Econvalue.class);
        query.setParameters(ecoV,periodSelec);
        resultCol = (Collection) pm.executeQuery(query);
        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;

    }

    public Collection<Teese03Econvalue> economicValuesMaxPeriodQuery(
            Teese03Econvalue ecoV) throws PersistenceException {
        String METHOD_NAME = "findEconomicValuesMaxPeriodQuery";
        log.entering(CLASS_NAME, METHOD_NAME);
        Collection<Teese03Econvalue> resultCol = null;
        PersistenceManager pm = null;
        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findEconomicValuesMaxPeriodQuery",
                Teese03Econvalue.class);
        query.setParameters(ecoV);
        resultCol = (Collection) pm.executeQuery(query);
        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;

    }
    
    /**
     * Devuelve el maximo effectiveDate asociado a los Economic Values asociados a "economic"
     * 
     * @param economic
     * @return
     * @throws PersistenceException 
     */
    public Timestamp findMaxEconomicValueEffDate(Teese01Economic economic) throws PersistenceException {
        String METHOD_NAME = "findMaxEconomicValueEffDate";
        log.entering(CLASS_NAME, METHOD_NAME);
                
        Timestamp result = null;
        PersistenceManager pm = null;
        HashMap<String, Timestamp> aux;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findMaxEconomicIndexValueEffDate", Teese03Econvalue.class);
        query.setParameters(economic);

        aux = (HashMap<String, Timestamp>) pm.executeQuery(query);
        
        result = aux.get("maxEffectiveDate");
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

}
